package com.vxb.testmvp.test_zonghe.test_1;

import android.content.Intent;
import android.graphics.Color;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.vxb.testmvp.R;
import com.vxb.testmvp.test_zonghe.entity.ZhuangEntity;
import com.vxb.testmvp.test_zonghe.network.NetworkManager;
import com.vxb.testmvp.test_zonghe.test_2.TestTowActivity;
import com.vxb.testmvp.util.Util;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

public class TestMActivity extends AppCompatActivity {

    @Bind(R.id.tv_error)
    TextView tv_error;
    @Bind(R.id.swipe_refresh)
    SwipeRefreshLayout swipe_refresh;
    @Bind(R.id.recycler_view)
    RecyclerView recycler_view;

    @Bind(R.id.ll_content)
    LinearLayout linearLayout;

    private ListAdapter adapter;
    private CompositeSubscription mCompositeSubscription;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_m);

        mCompositeSubscription = new CompositeSubscription();

        initView();
        initData();
    }

    @OnClick(R.id.tv_error)
    void refresh() {
        initData();

    }


    private void initData() {

        swipe_refresh.setRefreshing(true);
        tv_error.setVisibility(View.GONE);
        linearLayout.setVisibility(View.VISIBLE);

        mCompositeSubscription.add(NetworkManager.getZhuangbiApi()
                .search("装逼")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(
                        new Action1<List<ZhuangEntity>>() {
                            @Override
                            public void call(List<ZhuangEntity> zhuangEntities) {
                                adapter.setItems(zhuangEntities);
                                swipe_refresh.setRefreshing(false);

                                //if(数据集合= 0 ) 显示无数据 提示无数据页面  数据都缓存到文件
                                // 只有第一次 会展示错误, 只要进去过的页面, 不会显示错误. 都会展示缓存
                                tv_error.setVisibility(View.GONE);
                                linearLayout.setVisibility(View.VISIBLE);


                            }
                        },
                        new Action1<Throwable>() {

                            @Override
                            public void call(Throwable throwable) {
                                throwable.printStackTrace();

                                if (adapter.getItemCount() != 0) {
                                    Util.showToast(TestMActivity.this, "网络错误");
                                } else {
                                    tv_error.setVisibility(View.VISIBLE);
                                    linearLayout.setVisibility(View.GONE);
                                }
                                swipe_refresh.setRefreshing(false);
                            }
                        }));


    }

    private void initView() {
        ButterKnife.bind(this);
        swipe_refresh.setOnRefreshListener(new MyOnRefreshListener());//:设置手势滑动监听器。
        swipe_refresh.setColorSchemeColors(Color.BLUE, Color.GREEN, Color.RED, Color.YELLOW);//:设置进度圈的背景色。
        // swipe_refresh.setColorSchemeResources(int… colorResIds)//:设置进度动画的颜色。
        // swipe_refresh.setRefreshing(Boolean refreshing)//:设置组件的刷洗状态。

        adapter = new ListAdapter();
        recycler_view.setLayoutManager(new GridLayoutManager(this, 2));
        recycler_view.setAdapter(adapter);

        adapter.setOnItemClickLitener(new ListAdapter.OnItemClickLitener() {
            @Override
            public void onItemClick(View view, int position) {
                startActivity(new Intent( TestMActivity.this,TestTowActivity.class));
            }
        });

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mCompositeSubscription.hasSubscriptions()) {
            mCompositeSubscription.unsubscribe();
        }
    }

    class MyOnRefreshListener implements SwipeRefreshLayout.OnRefreshListener {

        @Override
        public void onRefresh() {
            initData();
        }
    }

}
